<?php
class SettingAction extends CommonAction{
	function getModel(){
		return D("Setting");
	}	
	
	function showlist(){
		$action=isset($_REQUEST['action'])?trim($_REQUEST['action']):'list';
		
		switch ($action){
			case 'list':
				if(!$this->isPost()){
					$m=$this->getModel();
					//获取所有分类
					$syslist=$m->where("issystem=1")->select();
					$diylist=$m->where("issystem=0")->select();
					
					$this->assign('syslist',$syslist);
					$this->assign('diylist',$diylist);
		
					$this->display("showlist");
				}else{
					C("TOKEN_ON",false);
					
					$m=D("Setting");
					foreach ($_POST['var'] as $k => $v)
					{
						$array=array(
							'Id'	=> $k,
							'varvalue'	=> $v,
						);
						
						if($m->create($array))
						{
							$m->save();
						}else{
							//出错
						}
					}
					
					$this->success("保存成功");
				}
				break;
			case 'add':
				if(!$this->isPost()){
					$this->display("edit");
				}else{
					$m=D("Setting");
					if($m->create()){
						$id=$m->add();
						if($id){
							$this->assign('jumpUrl',U("/Setting/showlist"));
							$this->success("添加成功");
						}else{
							$this->error($m->getDbError());
						}
					}else{
						$this->error($m->getError());
					}
				}
				break;
			case 'edit':
				if(!$this->isPost()){
					$m=$this->getModel();
					$data=$m->getById(intval($_REQUEST['Id']));
					$this->assign('data',$data);
					//print_r($data);
					$this->display('edit');
				}else{
					$m=D("Setting");
					if($m->create()){
						$row=$m->save();
						$this->assign('jumpUrl',U("/Setting/showlist"));
						$this->success("保存成功");
					}else{
						$this->error($m->getError());
					}
				}
				break;
		}
	}
}